/**
 * 
 */
package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.CmbB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CmbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2cMessage;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2cResMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * 招行B2c Service业务层处理对象
 * 
 * @CmsB2cBankServiceImpl.java
 * @author Ryan
 * @2013-10-29 上午11:21:05 www.gopay.com.cn Inc.All rights reserved.
 */
public class CmbB2cBankServiceImpl implements BankInteractRemoteService {

	@Resource(name = "cmbB2cManager")
	private CmbB2cManager cmbB2cManager;

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			CmbB2cMessage messageB2C = new CmbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			cmbB2cManager.buildMessageObj(messageB2C);
			CmbB2cMessage cmbB2cMessage = (CmbB2cMessage) cmbB2cManager
					.sign(messageB2C);
			result.setDefaultModel(cmbB2cMessage);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		boolean flag = false;
		try {
			CmbB2cMessage messageB2C = new CmbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			flag = (Boolean) cmbB2cManager.verify(messageB2C);
			result.setModel("message", messageB2C);
			result.setDefaultModel(flag);
			result.setSuccess(true);

			if ((Boolean) result.getDefaultModel()) {
				Map<String, String> param = new HashMap<String, String>();
				// 银行返回成功状态
				if (CmbConstants.CMB_PAY_STATUS_SUCCESS.equals(messageB2C
						.getSucceed())) {
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "00");
					param.put("retCode", "00");
					param.put("retMsg", "验签成功");
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", String.valueOf(base.isFront()));
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(true);
				} else {
					// 通知后台地址该订单失败
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "01");
					param.put("retCode", "01");
					param.put("retMsg", "验签失败");
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", String.valueOf(base.isFront()));
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(false);
				}
			}

		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			CmbB2cMessage messageB2C = new CmbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			CmbB2cResMessage res = (CmbB2cResMessage) cmbB2cManager
					.query(messageB2C);
			result.setSuccess(true);
			result.setDefaultModel(res);
			if (BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())) {
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getAmount().toString());
				if (res == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if (!CmbConstants.CMB_QUERY_STATUS_SUCCESS_INT.equals(res
						.getQueryResult())) {
					// 查询订单错误
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", res.getErrorMsg());
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				// 成功处理
				if (StringUtils.equals(CmbConstants.CMB_ORDER_STATUS_SUCCESS,
						res.getOrderStatus())) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
					param.put("returnMessage", "交易成功");
				} else {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					if (res.getErrorMsg() != null
							|| !"".equals(res.getErrorMsg())) {
						param.put("tradeState",
								BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", res.getErrorMsg());
					} else {
						param.put("tradeState",
								BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "招行订单查询未明确");
					}
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result refundBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result refundQuery(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
